/*
* ============================================================================
* 功能描述 ：中断
* 作者       ：ggls
* 创建日期 ：2024.03.27
* ----------------------------------------------------------------------------
* 更新历史 ：
*	日期 　　- 修改者   : 更新内容描述
*
* ============================================================================
*/

#include "main.h"
#include <sc.h>


#include <reg52.h>
 
typedef unsigned int u16;
typedef unsigned char u8;
 
// Wheel 0
sbit  P1_0 = P1^0;
sbit  P1_1 = P1^1;
// Wheel 1
sbit  P1_2 = P1^2;
sbit  P1_3 = P1^3;
// Wheel 2
sbit  P1_4 = P1^4;
sbit  P1_5 = P1^5;
// Wheel 3
sbit  P1_6 = P1^6;
sbit  P1_7 = P1^7;
 
/*
Duty Cycle =  Toogle_P1_x / PWM_Period；
*/
u8 PWM_Period = 128; // PWM Period = N * Timer delay(100us), between 10 - 254
u8 Toggle_W0 = 0; // Toggle of Wheel 0
u8 Dir_W0 = 0;    // Direction, 0:P1_0=0,P1_1=PWM, 1:P1_1=0,P1_0=PWM
u8 Toggle_W1 = 0; // Toggle of Wheel 1
u8 Dir_W1 = 0;    // Direction, 0:P1_2=0,P1_3=PWM, 1:P1_3=0,P1_2=PWM
 
u8 Count_W0, Count_W1;
 
void Time0_Init(void)
{
  TMOD = 0x02; // Mode 2, 8-bit and auto-reload
  TH0 = 0x9C;  // 0x9c = 156, timer of 100us (12MHz OSC)
  TL0 = 0x9C;
  ET0 = 1;
  EA  = 1;
  TR0 = 1;
 
  EX0 = 1; EX1 = 1; // Enable external interrupt 0 and 1
  IT0 = 1; IT1 = 1; // Toggle = jump
}
 
void main()
{
  Time0_Init();
  while(1);
}
 
void Timer0_IT() interrupt 1
{
  // W0
  if(Count_W0 == Toggle_W0) {
    if (Dir_W0 == 0) { // P1_1=PWM
      P1_1 = 0;
    } else { // P1_0=PWM
      P1_0 = 0;
    }
  }
  if(Count_W0 == PWM_Period - 1) {
    Count_W0 = 0;
    if (Dir_W0 == 0) {
      P1_0 = 0;
      P1_1 = 1;
    } else {
      P1_0 = 1;
      P1_1 = 0;
    }
  } else {
    Count_W0++;
  }
 
  // W1
  if(Count_W1 == Toggle_W1) {
    if (Dir_W1 == 0) { // P1_3=PWM
      P1_3 = 0;
    } else { // P1_2=PWM
      P1_2 = 0;
    }
  }
  if(Count_W1 == PWM_Period - 1) {
    Count_W1 = 0;
    if (Dir_W1 == 0) {
      P1_2 = 0;
      P1_3 = 1;
    } else {
      P1_2 = 1;
      P1_3 = 0;
    }
  } else {
    Count_W1++;
  }
 
}
 
 
// W0 dir0->max
void W0_dir0(void)
{
  if (Dir_W0 == 0) {
    Toggle_W0++;
    if(Toggle_W0 > PWM_Period) {
      Toggle_W0 = PWM_Period;
    }
  } else {
    Toggle_W0--;
    if(Toggle_W0 == 0) {
      Dir_W0 = 0;
    }
  }
}
 
// W0 dir1->max
void W0_dir1(void)
{
  if (Dir_W0 == 0) {
    Toggle_W0--;
    if(Toggle_W0 == 0) {
      Dir_W0 = 1;
    }
  } else {
    Toggle_W0++;
    if(Toggle_W0 > PWM_Period) {
      Toggle_W0 = PWM_Period;
    }
  }
}
 
// W1 dir0->max
void W1_dir0(void)
{
  if (Dir_W1 == 0) {
    Toggle_W1++;
    if(Toggle_W1 > PWM_Period) {
      Toggle_W1 = PWM_Period;
    }
  } else {
    Toggle_W1--;
    if(Toggle_W1 == 0) {
      Dir_W1 = 0;
    }
  }
}
// W1 dir1->max
void W1_dir1(void)
{
  if (Dir_W1 == 0) {
    Toggle_W1--;
    if(Toggle_W1 == 0) {
      Dir_W1 = 1;
    }
  } else {
    Toggle_W1++;
    if(Toggle_W1 > PWM_Period) {
      Toggle_W1 = PWM_Period;
    }
  }
}
 
 
void IT0_INT() interrupt 0
{
  W1_dir0();
}
 
void IT1_INT() interrupt 2
{
  W1_dir1();
}


